import re, sys, io, os
p = r'static/js/admin.js'
with io.open(p, 'r', encoding='utf-8') as f:
    s = f.read()
orig = s
# 1) data() additions after orders: createMockOrders(),
s = s.replace("orders: createMockOrders(),", "orders: createMockOrders(),\n      ordersTotal: 0,\n      ordersRaw: null,\n      ordersPagination: { pageIndex: 1, pageSize: 12, pages: 1 },\n      showOrdersRaw: false,")
# 2) dashboardStats use ordersTotal if available
s = s.replace("value: this.orders.length", "value: this.ordersTotal || this.orders.length")
# 3) fetchOrders: add params and capture raw/total/pages
# replace axios.get without params
s = s.replace("const { data } = await axios.get('/api/dashboard/orders');", "const params = { pageIndex: this.ordersPagination.pageIndex, pageSize: this.ordersPagination.pageSize };\n        const { data } = await axios.get('/api/dashboard/orders', { params });")
# inject ordersRaw/Total before end of if block inside fetchOrders
pattern = r"(async\\s+fetchOrders\\(\\)\\s*\\{[\\s\\S]*?if \\(data \\&\\& data\\.data \\&\\& Array\\.isArray\\(data\\.data\\.items\\)\\) \\\{[\\s\\S]*?\\}\s*)\n\s*}\s*catch"
m = re.search(pattern, s)
if m:
    block = m.group(1)
    # find end of the if { ... } block inside
    idx = block.rfind('}')
    if idx != -1:
        inject = "\n          this.ordersRaw = data.data.raw || null;\n          if (this.ordersRaw) {\n            const total = Number(this.ordersRaw.recordCount) || 0;\n            this.ordersTotal = total;\n            this.ordersPagination.pages = Number(this.ordersRaw.pageCount) || Math.max(1, Math.ceil(total / this.ordersPagination.pageSize));\n          }\n"
        newblock = block[:idx] + inject + block[idx:]
        s = s.replace(block, newblock)
# 4) methods: add helpers before mounted()
methods_inject = "\n    toggleOrdersRaw() {\n      this.showOrdersRaw = !this.showOrdersRaw;\n    },\n    changeOrdersPage(dir) {\n      const newPage = this.ordersPagination.pageIndex + dir;\n      if (newPage >= 1 && newPage <= this.ordersPagination.pages) {\n        this.ordersPagination.pageIndex = newPage;\n        this.fetchOrders();\n      }\n    },\n    handleOrdersPageSizeChange() {\n      this.ordersPagination.pageIndex = 1;\n      this.fetchOrders();\n    },\n"
s = s.replace("  },\n  mounted() {", methods_inject + "  },\n  mounted() {")
# 5) template: insert pager + raw JSON before closing of orders section
mt = re.search(r"template:\s*`([\\s\\S]*?)`", s)
if mt:
    tpl = mt.group(1)
    start = tpl.find("<section v-if=\"activeTab === 'orders'\"")
    if start != -1:
        after = tpl.find("</section>", start)
        if after != -1:
            extra = "\n        <div class=\"pager-row\" style=\"display:flex; gap:12px; align-items:center; margin-top:12px;\">\n          <span>每页条数:</span>\n          <select v-model=\"ordersPagination.pageSize\" @change=\"handleOrdersPageSizeChange\">\n            <option :value=\"10\">10</option>\n            <option :value=\"12\">12</option>\n            <option :value=\"20\">20</option>\n            <option :value=\"50\">50</option>\n            <option :value=\"100\">100</option>\n          </select>\n          <button class=\"btn\" type=\"button\" @click=\"changeOrdersPage(-1)\" :disabled=\"ordersPagination.pageIndex <= 1\">上一页</button>\n          <span>第 {{ ordersPagination.pageIndex }} / {{ ordersPagination.pages }} 页</span>\n          <button class=\"btn\" type=\"button\" @click=\"changeOrdersPage(1)\" :disabled=\"ordersPagination.pageIndex >= ordersPagination.pages\">下一页</button>\n          <button class=\"btn secondary\" type=\"button\" @click=\"toggleOrdersRaw\">{{ showOrdersRaw ? '隐藏原始结构' : '查看原始标准结构' }}</button>\n        </div>\n        <div v-if=\"showOrdersRaw\" class=\"log-block\" style=\"max-height: 280px; margin-top: 12px;\">\n          <pre class=\"log-line\">{{ JSON.stringify(ordersRaw && ordersRaw.recordList ? ordersRaw.recordList : [], null, 2) }}</pre>\n        </div>\n"
            newtpl = tpl[:after] + extra + tpl[after:]
            s = s.replace(tpl, newtpl)

if s != orig:
    with io.open(p, 'w', encoding='utf-8', newline='\n') as f:
        f.write(s)
print('OK')
